<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
  xmlns:dssp="http://schemas.microsoft.com/xw/2004/10/dssp.html"
  xmlns:vex="http://schemas.vexrobotics.com/2011/09/vex.html"
  xmlns:fn="http://www.w3.org/2005/02/xpath-functions/"
  >
  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:comment>
      <!-- Service Header Info -->
    </xsl:comment>
    <xsl:variable name="title">
      VEX Service
    </xsl:variable>
    <xsl:variable name="serviceName">
      VEX
    </xsl:variable>
    <xsl:variable name="description">
      Provides access to the VEX service.
    </xsl:variable>

    <xsl:call-template name="MasterPage">
      <xsl:with-param name="serviceName" select="$serviceName" />
      <xsl:with-param name="description" select="$description" />
      <!-- If title is not provided, serviceName will be used instead. -->
      <xsl:with-param name="title">
        <xsl:value-of select="$serviceName" />
        <xsl:if test="$title != ''">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$title" />
        </xsl:if>
      </xsl:with-param>

      <!-- Possible values for navigation are: 'Open', 'Closed', and 'None'
           'Open' is the default value. - ->
      <xsl:with-param name="navigation" select="'Open'" />
      -->
      <!-- The contents of head param will be placed just before the </head> tag in html. -->
      <xsl:with-param name="head">
        <style type="text/css">
          /* Service-specific stylesheet goes here */
        </style>
        <script language="javascript" type="text/javascript" src="/resources/VEXService.Y2011.M09/VEXService.config.Common.js"/>
        <script language="javascript" type="text/javascript">
          <xsl:text>
var pollingFrequency = 1000;
</xsl:text>

          <![CDATA[
function onState(state)
{
    var i;
    var cnt;

  {
    var motors = state.Motors.Motors;
    cnt = motors["MotorData_Cnt"];
    for(i=1; i<=cnt; ++i)
    {
      var aMotor = motors["MotorData_" + i];
      document.all("motorCell_" + i).innerText = aMotor.Value;
    }
  }

  {
    var sensors = state.PortSensors.Sensors;
    cnt = sensors["PortSensorData_Cnt"];
    for(i=1; i<=cnt; ++i)
    {
      var aSensor = sensors["PortSensorData_" + i];
      document.all("portCell_" + i).innerText = aSensor.Value;
    }
  }

  {
    var sensors = state.InterruptSensors.Sensors;
    cnt = sensors["InterruptSensorData_Cnt"];
    for(i=1; i<=cnt; ++i)
    {
      var aSensor = sensors["InterruptSensorData_" + i];
      document.all("interruptCell_" + i).innerText = aSensor.Value;
    }
  }

  readState(pollingFrequency);
}

function startPolling()
{
  setStateCompletion("onState");
  readState(pollingFrequency);
}

setTimeout("startPolling()", pollingFrequency);

          ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="/vex:VEXState">
    <!-- Warn if not actually connected -->
    <xsl:if test="vex:Connected = 'NotConnected'">
      <p style="color:red">VEX is not connected. Please configure below.</p>
    </xsl:if>
    <xsl:if test="vex:Connected = 'ConnectedVEXToPc'">
      <p style="color:red">Connection is open, but invalid response obtained. Please configure below.</p>
    </xsl:if>

    <!-- Display the COM port information -->
    <!-- Use a form to allow setting only the port -->
    <form method="POST" action="" name="ConfigurationForm">

      <table width="100%">
        <th colspan="2">
        <img src="/resources/VEX.Y2011.M09/VEXRobotics.Microcontroller.jpg"
               align="left" alt="Controller" width="124px" height="124px"/>
        </th>
        <!-- Put up the banner -->
        <tr>
          <th colspan="2" class="Major">Configuration</th>
        </tr>

        <!-- Display the COM port -->
        <xsl:call-template name="vex:SerialPortConfig"/>

        <!-- Allow the user to change it -->
        <input type="hidden" name="Action" value="VEXConfig" />
        <tr>
          <td>
            <input id="Button2" type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
          </td>
          <td>
            <input id="Button1" type="submit" value="Connect" name="buttonOk" title="Update and Connect"/>
          </td>
        </tr>
      </table>
    </form>

    <form method="POST" action="" name="SetMotorForm">
      <table width="100%">
        <!-- Motor display -->
        <tr>
          <!-- Another form to isolate just the motor settings -->
          <input type="hidden" name="Action" value="VEXMotorOutputs" />
          <td colspan="2">
            <table width="100%">
              <tr>
                <th>Motor Configuration</th>
              </tr>

              <!-- Display the motors -->
              <xsl:variable name="MotorSensorTypesList" select="vex:AvailableMotorSensorTypes" />
              <xsl:for-each select="vex:Motors/vex:Motors/vex:MotorData">
                <xsl:call-template name="vex:MotorConfigRow">
                  <xsl:with-param name="List" select="$MotorSensorTypesList" />
                </xsl:call-template>
              </xsl:for-each>
              <tr>
                <!-- Allow the user to do simple things -->
                <td style="text-align:left;">
                  <input type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>

                  <input type="submit" value="All Stop" name="button_stop_all" title="Stop All Motors"/>

                  <input type="submit" value="Set All Motors" name="button_set_all" title="Set All Motors"/>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </form>

    <form method="POST" action="" name="SetPortsForm">
      <table width="100%">
        <!-- Port Sensor display -->
        <tr>
          <!-- Another form to isolate just the port settings -->
          <input type="hidden" name="Action" value="VEXIOPorts" />
          <td colspan="2">
            <table width="100%">
              <tr>
                <th>IO Port Sensor Configuration</th>
              </tr>

              <!-- Display the port settings/values -->
              <xsl:variable name="IOSensorTypesList" select="vex:AvailablePortSensorTypes" />
              <xsl:for-each select="vex:PortSensors/vex:Sensors/vex:PortSensorData">
                <xsl:call-template name="vex:PortSensorConfigRow">
                  <xsl:with-param name="List" select="$IOSensorTypesList" />
                </xsl:call-template>
              </xsl:for-each>

              <!-- Additional simple operations -->
              <td style="text-align:left;">
                <input type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
                <input type="submit" value="Set All Ports" name="button_set_all" title="Set All Ports"/>
              </td>
            </table>
          </td>
        </tr>
      </table>
    </form>

    <form method="POST" action="" name="SetIntsForm">
      <table width="100%">
        <!-- Interrupt Sensor display -->
        <tr>
          <!-- Another form to isolate just the interrupt settings -->
          <input type="hidden" name="Action" value="VEXInterupts" />
          <td colspan="2">
            <table width="100%">
              <tr>
                <th>Interupt Sensor Configuration</th>
              </tr>

              <!-- Display the interrupt settings/values -->
              <xsl:variable name="InteruptSensorTypesList" select="vex:AvailableInterruptSensorTypes" />
              <xsl:variable name="IOSensorTypesList" select="vex:AvailablePortSensorPorts" />
              <xsl:for-each select="vex:InterruptSensors/vex:Sensors/vex:InterruptSensorData">
                <xsl:call-template name="vex:InteruptSensorConfigRow">
                  <xsl:with-param name="List" select="$InteruptSensorTypesList" />
                  <xsl:with-param name="PortList" select="$IOSensorTypesList" />
                </xsl:call-template>
              </xsl:for-each>

              <!-- Additional simple operations -->
              <td style="text-align:left;">
                <input id="Button2" type="reset" value="Reset" name="buttonReset" title="Reset Configuration"/>
                <input type="submit" value="Set All Interupts" name="button_set_all" title="Set All Interupts"/>
              </td>
            </table>
          </td>
        </tr>
      </table>
    </form>
  </xsl:template>

  <!-- Simple template to display the communications settings -->
  <xsl:template name="vex:SerialPortConfig">
    <tr>
      <!-- If not set, color the setting -->
      <xsl:if test="vex:ComPort = '0'">
        <xsl:attribute name="style">
          <xsl:text>background:red;</xsl:text>
        </xsl:attribute>
      </xsl:if>

      <!-- COM port value -->
      <td size="10">COM Port</td>
      <td align="right" size="21">
        <input type="text" name="ComPort" size="5">
          <xsl:attribute name="value">
            <xsl:value-of select="vex:ComPort"/>
          </xsl:attribute>
        </input>
      </td>
    </tr>
  </xsl:template>

  <!-- Simple template for the motor port settings -->
  <xsl:template name="vex:MotorConfigRow">
    <xsl:param name="List" />
    <xsl:variable name="ThisRowVal" select="vex:Type"/>
    <tr>
      <td>
        <table width="100%">
          <tr>
            <th rowspan="5" class="Left">
              <b>
                <xsl:value-of select="position()"/>
              </b>
            </th>
          </tr>

          <!-- Display a list of the types the motor can be -->
          <tr>
            <td width="7%">Type</td>
            <td width="23%">
              <select size="1">
                <xsl:attribute name="name">
                  <xsl:text>MotorSensorType</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:for-each select="$List/vex:string">
                  <xsl:element name="option">
                    <xsl:if test="$ThisRowVal = .">
                      <xsl:attribute name="selected">true</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="."/>
                  </xsl:element>
                </xsl:for-each>
              </select>
            </td>

            <!-- Display the value set for the motor -->
            <td width="7%">Value</td>
            <td width="8%">
              <xsl:attribute name="id">
                <xsl:value-of select="'motorCell_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:attribute name="value">
                <xsl:value-of select="vex:Value"/>
              </xsl:attribute>
            </td>

            <td width="15%">
              <input type="text" size="5">
                <xsl:attribute name="name">
                  <xsl:text>Value</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="value">
                  <xsl:value-of select="vex:Value"/>
                </xsl:attribute>
              </input>
            </td>

            <!-- Button to set the type/value -->
            <td width="45%">
              <input type="submit" value=" Set ">
                <xsl:attribute name="name">
                  <xsl:value-of select="'button_set_'"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:value-of select="'Set motor '"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
              </input>
            </td>

          </tr>

        </table>
      </td>
    </tr>

  </xsl:template>


  <!-- Simple template for the IO port settings -->
  <xsl:template name="vex:PortSensorConfigRow">
    <xsl:param name="List" />
    <xsl:variable name="ThisRowVal" select="vex:Type"/>
    <tr>
      <td>
        <table width="100%">
          <tr>
            <th rowspan="6" class="Left">
              <b>
                <xsl:value-of select="position()"/>
              </b>
            </th>

            <!-- Display the types of usage of a port -->
            <td width="7%">Type</td>
            <td width="20%">
              <select size="1">
                <xsl:attribute name="name">
                  <xsl:text>PortSensorType</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>

                <xsl:for-each select="$List/vex:string">

                  <xsl:element name="option">
                    <xsl:if test="$ThisRowVal = .">
                      <xsl:attribute name="selected">true</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="."/>
                  </xsl:element>
                </xsl:for-each>
              </select>
            </td>
            <td width="73%"></td>
          </tr>

          <!-- Display the current value -->
          <tr>
            <td width="7%">Value</td>
            <td width="8%">
              <xsl:attribute name="id">
                <xsl:value-of select="'portCell_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:value-of select="vex:Value"/>
            </td>
            <td width="15%">
              <xsl:element name="input">
                <xsl:attribute name="type">text</xsl:attribute>
                <xsl:attribute name="size">5</xsl:attribute>
                <xsl:attribute name="name">
                  <xsl:text>Value</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="size">20</xsl:attribute>
                <xsl:attribute name="value">
                  <xsl:value-of select="vex:Value"/>
                </xsl:attribute>
              </xsl:element>
            </td>
            <td width="70%"></td>
          </tr>

          <!-- Button to set the type/value -->
          <tr>
            <td width="7%"></td>
            <td width="8%">
              <input type="submit" value=" Set ">
                <xsl:attribute name="name">
                  <xsl:value-of select="'button_set_'"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:value-of select="'Set port '"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
              </input>
            </td>
          </tr>

        </table>
      </td>
    </tr>
  </xsl:template>

  <!-- Simple template for the interrupt port settings -->
  <xsl:template name="vex:InteruptSensorConfigRow">
    <xsl:param name="List" />
    <xsl:param name="PortList" />
    <xsl:variable name="ThisRowVal" select="vex:Type"/>
    <tr>
      <td>
        <table width="100%">
          <tr>
            <th rowspan="6" class="Left">
              <b>
                <xsl:value-of select="position()"/>
              </b>
            </th>
          </tr>

          <!-- Display a list of the types the interrupt can be -->
          <tr>
            <td width="7%">Type</td>
            <td width="20%">
              <select size="1">
                <xsl:attribute name="name">
                  <xsl:text>InteruptSensorType</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>

                <xsl:for-each select="$List/vex:string">

                  <xsl:element name="option">
                    <xsl:if test="$ThisRowVal = .">
                      <xsl:attribute name="selected">true</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="."/>
                  </xsl:element>
                </xsl:for-each>
              </select>
            </td>
            <td width="73%">
            </td>
          </tr>

          <!-- For sonar sensors, we need a port number -->
          <tr>
            <td width="7%">Port</td>
            <td width="20%">
              <select size="1">
                <xsl:attribute name="name">
                  <xsl:text>Port</xsl:text>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>

                <xsl:for-each select="$PortList/vex:string">

                  <xsl:element name="option">
                    <xsl:if test="$ThisRowVal = .">
                      <xsl:attribute name="selected">true</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="."/>
                  </xsl:element>
                </xsl:for-each>
              </select>
            </td>
          </tr>

          <!-- Display the current value associated with the port -->
          <tr>
            <td width="7%">Value</td>
            <td width="20%">
              <xsl:attribute name="id">
                <xsl:value-of select="'interruptCell_'"/>
                <xsl:value-of select="position()"/>
              </xsl:attribute>
              <xsl:value-of select="vex:Value"/>
            </td>
          </tr>

          <!-- Button to set the type -->
          <tr>
            <td width="7%"></td>
            <td width="20%">
              <input type="submit" value=" Set ">
                <xsl:attribute name="name">
                  <xsl:value-of select="'button_set_'"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
                <xsl:attribute name="title">
                  <xsl:value-of select="'Set port '"/>
                  <xsl:value-of select="position()"/>
                </xsl:attribute>
              </input>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
